package com.example.applets.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.applets.entity.Admin;
import com.example.applets.entity.User;
import com.example.applets.service.impl.AdminServiceImpl;
import com.example.applets.service.impl.UserServiceImpl;
import com.example.applets.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author 云烟成雨丶
 * @since 2020-10-09
 */
@Controller
@RequestMapping("/applets/admin")
public class AdminController {
    @Autowired
    private AdminServiceImpl adminService;

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/getOne")
    @ResponseBody
    //用户验证
    public AjaxResult getOne(@RequestParam("uname") String uname, @RequestParam("upass") String upass, @RequestParam("radio") int radio) {
        if (radio == 1) {
            QueryWrapper<Admin> wrapperAdmin = new QueryWrapper<>();
            wrapperAdmin.eq("uname", uname);
            wrapperAdmin.eq("upass", upass);
            Admin admin = adminService.getOne(wrapperAdmin);
            if (admin == null) {
                return AjaxResult.error("名称或者密码出错!");
            } else if (!admin.getInuse()) {
                return AjaxResult.error("暂无权限!");
            }
            return AjaxResult.success("登录成功!", "menu.html");
        }
        QueryWrapper<User> wrapperUser = new QueryWrapper<>();
        wrapperUser.eq("wname", uname);
        wrapperUser.eq("upass", upass);
        User user = userService.getOne(wrapperUser);
        if (user == null) {
            return AjaxResult.error("名称或者密码出错!");
        } else if (!user.getInuse()) {
            return AjaxResult.error("暂无权限!");
        }
        return AjaxResult.success("登录成功!", "/kUser/UserMenu.html?userId=" + user.getId());

    }
}

